package com.zxu.test.view;

import android.content.Context;
import android.os.CountDownTimer;
import android.os.Handler;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.zxu.test.application.MyApplication;
import com.zxu.test.studyapp.R;

public class ToastCustom {
    private boolean canceled = true;
    private Handler handler;
    private Toast toast;
    private TimeCount time;
    private TextView toast_content;
    //private TextView toast_content;
    private static ToastCustom instance;
    public static ToastCustom getInstance(Context context) {
        if (instance == null) {
            instance = new ToastCustom(context);
        }
        return instance;
    }
    public ToastCustom(Context context) {
        this(context, new Handler());
    }
    public ToastCustom(Context context, Handler handler) {
        this.handler = handler;
        View layout = LayoutInflater.from(context).inflate(R.layout.toast_tips_bg, null, false);
        toast_content = (TextView) layout.findViewById(R.id.tvToastContent);
        if (toast == null) {
            toast = new Toast(context);
        }
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.setDuration(Toast.LENGTH_LONG);
        toast.setView(layout);
    } /**
     * @param text
     * 要显示的内容
     * * @param duration 显示的时间长
     * *
     * 根据LENGTH_MAX进行判断
     * *
     * 如果不匹配，进行系统显示
     * *
     * 如果匹配，永久显示，直到调用hide()
     * */
    public void show(String text, int duration) {
        Context context = MyApplication.getInstance();
        if (toast != null) {
            toast = null;
        }
        toast = new Toast(context);
        time = new TimeCount(duration, 100); //1000是消失渐变时间
        View layout = LayoutInflater.from(context).inflate(R.layout.toast_tips_bg, null, false);
        toast_content = (TextView) layout.findViewById(R.id.tvToastContent);
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.setDuration(Toast.LENGTH_LONG);
        toast.setView(layout);

        toast_content.setText(text);
        time.start();
        toast.show();
    } /**     * 隐藏Toast     */
    public void hide() {
        if (toast != null) {
            toast.cancel();
            time.cancel();
        }
        canceled = true;
    }
    /**     * 计时器     */
    class TimeCount extends CountDownTimer {
        public TimeCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval); // 总时长,计时的时间间隔
        }
        @Override public void onFinish() { // 计时完毕时触发
            hide();
        }
        @Override
        public void onTick(long millisUntilFinished) { // 计时过程显示
        }
    }
}
